// ========================================================================= //

// includes
#include "stdio.h"
#include "stdlib.h"
#include "time.h"
#include "string.h"
#include "limits.h"
#include "float.h"



#define JOTAI_NUM_RANDS_ 25

const unsigned rand_primes[JOTAI_NUM_RANDS_] = {179, 103, 479, 647, 229, 37, 271, 557, 263, 607, 18743, 50359, 21929, 48757, 98179, 12907, 52937, 64579, 49957, 52567, 507163, 149939, 412157, 680861, 757751};

int next_i() {
  int counter = 0;
  return rand_primes[(++counter)%JOTAI_NUM_RANDS_];
}

float next_f() {
  int counter = 0;
  return rand_primes[(++counter)%JOTAI_NUM_RANDS_] / 757751.0F;
} 


// Usage menu
void usage() {
    printf("%s", "Usage:\n\
    prog [ARGS]\n\
\nARGS:\n\
       0            int-bounds\n\
       1            big-arr\n\
       2            big-arr-10x\n\
       3            empty\n\
\n\
");

}


// ------------------------------------------------------------------------- //

#define NULL ((void*)0)
typedef unsigned long size_t;  // Customize by platform.
typedef long intptr_t; typedef unsigned long uintptr_t;
typedef long scalar_t__;  // Either arithmetic or pointer type.
/* By default, we understand bool (as a convenience). */
typedef int bool;
#define false 0
#define true 1

/* Forward declarations */

/* Type definitions */
typedef  int u32 ;
struct xhci_hcd {int quirks; } ;

/* Variables and functions */
 int PORT_CAS ; 
 int PORT_PLS_MASK ; 
 int USB_PORT_STAT_CONNECTION ; 
 int USB_SS_PORT_LS_COMP_MOD ; 
 int USB_SS_PORT_LS_SS_INACTIVE ; 
 int USB_SS_PORT_LS_U3 ; 
 int XDEV_RESUME ; 
 int XHCI_COMP_MODE_QUIRK ; 

__attribute__((used)) static void xhci_hub_report_usb3_link_state(struct xhci_hcd *xhci,
		u32 *status, u32 status_reg)
{
	u32 pls = status_reg & PORT_PLS_MASK;

	/* resume state is a xHCI internal state.
	 * Do not report it to usb core, instead, pretend to be U3,
	 * thus usb core knows it's not ready for transfer
	 */
	if (pls == XDEV_RESUME) {
		*status |= USB_SS_PORT_LS_U3;
		return;
	}

	/* When the CAS bit is set then warm reset
	 * should be performed on port
	 */
	if (status_reg & PORT_CAS) {
		/* The CAS bit can be set while the port is
		 * in any link state.
		 * Only roothubs have CAS bit, so we
		 * pretend to be in compliance mode
		 * unless we're already in compliance
		 * or the inactive state.
		 */
		if (pls != USB_SS_PORT_LS_COMP_MOD &&
		    pls != USB_SS_PORT_LS_SS_INACTIVE) {
			pls = USB_SS_PORT_LS_COMP_MOD;
		}
		/* Return also connection bit -
		 * hub state machine resets port
		 * when this bit is set.
		 */
		pls |= USB_PORT_STAT_CONNECTION;
	} else {
		/*
		 * If CAS bit isn't set but the Port is already at
		 * Compliance Mode, fake a connection so the USB core
		 * notices the Compliance state and resets the port.
		 * This resolves an issue generated by the SN65LVPE502CP
		 * in which sometimes the port enters compliance mode
		 * caused by a delay on the host-device negotiation.
		 */
		if ((xhci->quirks & XHCI_COMP_MODE_QUIRK) &&
				(pls == USB_SS_PORT_LS_COMP_MOD))
			pls |= USB_PORT_STAT_CONNECTION;
	}

	/* update status field */
	*status |= pls;
}

// ------------------------------------------------------------------------- //

int main(int argc, char *argv[]) {

    if (argc != 2) {
        usage();
        return 1;
    }

    int opt = atoi(argv[1]);
    switch(opt) {

    // int-bounds
    case 0:
    {
          int status_reg = 100;
        
          int _len_xhci0 = 1;
          struct xhci_hcd * xhci = (struct xhci_hcd *) malloc(_len_xhci0*sizeof(struct xhci_hcd));
          for(int _i0 = 0; _i0 < _len_xhci0; _i0++) {
              xhci[_i0].quirks = ((-2 * (next_i()%2)) + 1) * next_i();
        
          }
        
          int _len_status0 = 1;
          int * status = (int *) malloc(_len_status0*sizeof(int));
          for(int _i0 = 0; _i0 < _len_status0; _i0++) {
            status[_i0] = ((-2 * (next_i()%2)) + 1) * next_i();
          }
        
          xhci_hub_report_usb3_link_state(xhci,status,status_reg);
          free(xhci);
          free(status);
        
        break;
    }
    // big-arr
    case 1:
    {
          int status_reg = 255;
        
          int _len_xhci0 = 65025;
          struct xhci_hcd * xhci = (struct xhci_hcd *) malloc(_len_xhci0*sizeof(struct xhci_hcd));
          for(int _i0 = 0; _i0 < _len_xhci0; _i0++) {
              xhci[_i0].quirks = ((-2 * (next_i()%2)) + 1) * next_i();
        
          }
        
          int _len_status0 = 65025;
          int * status = (int *) malloc(_len_status0*sizeof(int));
          for(int _i0 = 0; _i0 < _len_status0; _i0++) {
            status[_i0] = ((-2 * (next_i()%2)) + 1) * next_i();
          }
        
          xhci_hub_report_usb3_link_state(xhci,status,status_reg);
          free(xhci);
          free(status);
        
        break;
    }
    // big-arr-10x
    case 2:
    {
          int status_reg = 10;
        
          int _len_xhci0 = 100;
          struct xhci_hcd * xhci = (struct xhci_hcd *) malloc(_len_xhci0*sizeof(struct xhci_hcd));
          for(int _i0 = 0; _i0 < _len_xhci0; _i0++) {
              xhci[_i0].quirks = ((-2 * (next_i()%2)) + 1) * next_i();
        
          }
        
          int _len_status0 = 100;
          int * status = (int *) malloc(_len_status0*sizeof(int));
          for(int _i0 = 0; _i0 < _len_status0; _i0++) {
            status[_i0] = ((-2 * (next_i()%2)) + 1) * next_i();
          }
        
          xhci_hub_report_usb3_link_state(xhci,status,status_reg);
          free(xhci);
          free(status);
        
        break;
    }
    // empty
    case 3:
    {
          int status_reg = ((-2 * (next_i()%2)) + 1) * next_i();
        
          int _len_xhci0 = 1;
          struct xhci_hcd * xhci = (struct xhci_hcd *) malloc(_len_xhci0*sizeof(struct xhci_hcd));
          for(int _i0 = 0; _i0 < _len_xhci0; _i0++) {
              xhci[_i0].quirks = ((-2 * (next_i()%2)) + 1) * next_i();
        
          }
        
          int _len_status0 = 1;
          int * status = (int *) malloc(_len_status0*sizeof(int));
          for(int _i0 = 0; _i0 < _len_status0; _i0++) {
            status[_i0] = ((-2 * (next_i()%2)) + 1) * next_i();
          }
        
          xhci_hub_report_usb3_link_state(xhci,status,status_reg);
          free(xhci);
          free(status);
        
        break;
    }
    default:
        usage();
        break;

    }

    return 0;
}
